package bbsimon.first.guess;

import java.io.*;
import java.util.Random;
import javax.servlet.*;
import javax.servlet.http.*;

public class GuessServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse res)
		throws IOException, ServletException {
			res.setContentType("text/html");
			PrintWriter out = res.getWriter();
			out.println("<html><head><title>Number Guess Game</title></head>");
			HttpSession session = req.getSession();
			
		
			String s = req.getParameter("restart");

			if(s.equals("yes")) {
				session.setAttribute("left", new Integer(10));
				Random ran = new Random();
				session.setAttribute("number", new Integer(ran.nextInt(100)));
				out.println("<h1>Game Start</h1><p>input a number between 0-100</p>"
						+ "<form method=\"GET\" action=\"guess\">"
						+ "<input name=\"guess\">"
						+ "<input type=\"HIDDEN\" name=\"restart\" value=\"no\" />"
						+ "<input type=\"SUBMIT\" value=\"Guess it\"/>"
						+ "</form><br><a href=\"guess?restart=yes\">Restart</a>"
						+ "<br><br><a href=\"./\">Back</a></body></html>");
			}
			else if(s.equals("no")){
				Integer guess;
				Integer left = (Integer)(session.getAttribute("left"));
				if(left == 0) {
					out.println("<h1>0 left for your game is over</h1></body><html>"
							+ "<a href=\"./guess?restart=yes\">Start another game</a>");
					return;
				}
				try {
					guess = Integer.parseInt(req.getParameter("guess"));
					if(guess < 0 || guess > 100)
						throw new Exception("guess number must between 0-100");
				}
				catch(NumberFormatException e) {
					out.println("<h1>You must input a number but a string or something else</h1><a href=\"guess?restart=continue"
							+ "\">Get back to continue</a><br><br><a href=\"guess?restart=yes\">Restart a new game<"
							+ "/a>");
					out.println("</body></html>");
					return;
				}
				catch(Exception e) {
					out.println("<h1>Guess number must between 0-100</h1><a href=\"guess?"
							+ "restart=continue\">Return to continue</a><br><br><a href=\"guess?"
							+ "restart=yes\">Restart a new game</a>");
					out.println("</body></html>");
					return;
				}
				Integer number = (Integer)(session.getAttribute("number"));
				if(guess < number)
					out.println("<h1>You guess is less</h1>");
				else if(guess > number)
					out.println("<h1>Your guess is greater</h1>");
				else
					out.println("<h1>You got the right number: " + number + "</h1>");
				left = left - 1;
				session.setAttribute("left", new Integer(left));
				out.println("<p>You have " + left + " chances left</p>"
						+ "<form method=\"GET\" action=\"guess\"><input name="
						+ "\"guess\"><input type=\"SUBMIT\" name=\"restart\" value=\""
						+ "no\"/><form>"
						+ "<br><a href=\"guess?restart=yes\">Restart</a></body>"
						+ "</html>");

			}
			else if(s.equals("continue")) {
				Integer left = (Integer)(session.getAttribute("left"));
				out.println("<h1>Continue guessing</h1><p>You have " + left + " chances left</p>"
						+ "<form method=\"GET\" action=\"guess\">"
						+ "<input type=\"TEXT\" name=\"guess\">"
						+ "<input name=\"restart\" type=\"HIDDEN\" value=\"no\"/>"
						+ "<input type=\"SUBMIT\" value=\"Guess\" />"
						+ "</form><br><a href=\"guess?restart=yes\">Restart</a></body>"
						+ "</html>");
			}
		}
}
